<route lang="json5">
{
  style: {
    navigationBarTitleText: '人员考评',
  },
}
</route>

<template>
  <view class="common-container">
    <view class="common-info">
      <view class="common-info-title">用户信息</view>
      <view class="common-info-content">
        <view class="common-info-item">考核用户：{{ detailData.userId_dictText }}</view>
        <view class="common-info-item">所属礼堂：{{ detailData.hallId_dictText }}</view>
        <view class="common-info-item">
          所属季度：{{ detailData.year }}年第{{ detailData.quarter }}季度
        </view>
        <view class="common-info-item">
          活动信息：{{ detailData.actualNum }}/{{ detailData.targetNum }}
        </view>
      </view>
    </view>
    <!-- 已经初评 -->
    <view
      v-if="detailData.status === 'toFinal' || detailData.status === 'complete'"
      class="common-info"
    >
      <view class="common-info-title">初评信息</view>
      <view class="common-info-content">
        <wd-form custom-class="common-form" :model="earlyScoreData">
          <wd-cell-group>
            <wd-input
              type="number"
              label="管理能力(1-70分)"
              label-width="130px"
              v-model="earlyScoreData.manage"
              placeholder=""
              readonly
            />
            <wd-input
              type="number"
              label="组织能力(1-15分)"
              label-width="130px"
              v-model="earlyScoreData.organization"
              placeholder=""
              readonly
            />
            <wd-input
              type="number"
              label="业务能力(1-15分)"
              label-width="130px"
              v-model="earlyScoreData.business"
              placeholder=""
              readonly
            />
            <wd-input
              type="number"
              label="加分项(0-20分)"
              label-width="130px"
              v-model="earlyScoreData.additional"
              placeholder=""
              readonly
            />
            <wd-cell title="重大扣分事项">
              <wd-textarea
                v-model="earlyScoreData.deductNote"
                auto-height
                placeholder=""
                readonly
              />
            </wd-cell>
            <wd-cell title="综合评价">
              <wd-textarea v-model="earlyScoreData.note" auto-height placeholder="" readonly />
            </wd-cell>
          </wd-cell-group>
        </wd-form>
      </view>
    </view>
    <!-- 已经终评 -->
    <view v-if="detailData.status === 'complete'" class="common-info">
      <view class="common-info-title">终评信息</view>
      <view class="common-info-content">
        <wd-form custom-class="common-form" :model="finalScoreData">
          <wd-cell-group>
            <wd-input
              type="number"
              label="管理能力(1-70分)"
              label-width="130px"
              v-model="finalScoreData.manage"
              placeholder=""
              readonly
            />
            <wd-input
              type="number"
              label="组织能力(1-15分)"
              label-width="130px"
              v-model="finalScoreData.organization"
              placeholder=""
              readonly
            />
            <wd-input
              type="number"
              label="业务能力(1-15分)"
              label-width="130px"
              v-model="finalScoreData.business"
              placeholder=""
              readonly
            />
            <wd-input
              type="number"
              label="加分项(0-20分)"
              label-width="130px"
              v-model="finalScoreData.additional"
              placeholder=""
              readonly
            />
            <wd-cell title="重大扣分事项">
              <wd-textarea
                v-model="finalScoreData.deductNote"
                auto-height
                placeholder=""
                readonly
              />
            </wd-cell>
            <wd-cell title="综合评价">
              <wd-textarea v-model="finalScoreData.note" auto-height placeholder="" readonly />
            </wd-cell>
          </wd-cell-group>
        </wd-form>
      </view>
    </view>
    <!-- 只要 “未完成” 就都需要审批 -->
    <view v-if="detailData.status !== 'complete'" class="common-info">
      <view class="common-info-title">审批</view>
      <view class="common-info-content">
        <wd-form custom-class="common-form" ref="formRef" :model="formModel">
          <wd-cell-group>
            <wd-input
              type="number"
              label="管理能力(1-70分)"
              label-width="130px"
              v-model="formModel.manage"
              placeholder="请输入"
              required
              :maxlength="2"
            />
            <wd-input
              type="number"
              label="组织能力(1-15分)"
              label-width="130px"
              v-model="formModel.organization"
              placeholder="请输入"
              required
              :maxlength="2"
            />
            <wd-input
              type="number"
              label="业务能力(1-15分)"
              label-width="130px"
              v-model="formModel.business"
              placeholder="请输入"
              required
              :maxlength="2"
            />
            <wd-input
              type="number"
              label="加分项(0-20分)"
              label-width="130px"
              v-model="formModel.additional"
              placeholder="请输入"
              required
              :maxlength="2"
            />
            <wd-cell title="重大扣分事项">
              <wd-textarea v-model="formModel.deductNote" auto-height placeholder="请输入" />
            </wd-cell>
            <wd-cell title="综合评价">
              <wd-textarea v-model="formModel.note" auto-height placeholder="请输入" />
            </wd-cell>
          </wd-cell-group>
        </wd-form>
        <wd-button type="primary" @click="submitForm" :round="false" block>提交</wd-button>
      </view>
    </view>
  </view>
</template>

<script setup>
import AccessApi from '@/service/access'

const formRef = ref()
const formModel = ref({
  manage: '',
  organization: '',
  business: '',
  additional: 0,
  deductNote: '',
  note: '',
})
const formKeyMap = {
  manage: {
    label: '管理能力',
    max: 70,
  },
  organization: {
    label: '组织能力',
    max: 15,
  },
  business: {
    label: '业务能力',
    max: 15,
  },
  additional: {
    label: '加分项',
    max: 20,
  },
}

const earlyScoreData = ref({
  manage: '',
  organization: '',
  business: '',
  additional: '',
  deductNote: 0,
  note: '',
})
const finalScoreData = ref({
  manage: '',
  organization: '',
  business: '',
  additional: '',
  deductNote: 0,
  note: '',
})

const detailData = ref({
  userId_dictText: '',
  hallId_dictText: '',
  year: '',
  quarter: '',
  targetNum: '',
  actualNum: '',
})

const getDetail = async (id) => {
  const params = { id }
  const { code, result } = await AccessApi.getPersonAccessDetail(params)
  if (code === 200 && result) {
    console.log('res', result)
    detailData.value = { ...result }
    if (result.status !== 'start') {
      const { code: code2, result: result2 } = await AccessApi.getPersonAccessTarget(params)
      if (code2 === 200 && result2?.length) {
        result2.forEach((item) => {
          earlyScoreData.value[item.target] = item.evaluationScore
          finalScoreData.value[item.target] = item.finalEvaluationScore
        })
        earlyScoreData.value.deductNote = result.directDeductNote
        finalScoreData.value.deductNote = result.superDeductNote
        earlyScoreData.value.note = result.directNote
        finalScoreData.value.note = result.superNote
      }
    }
  }
}

const submitForm = () => {
  let objKey = ''
  let max = ''
  for (const key in formKeyMap) {
    if (!formModel.value[key]) {
      objKey = key
      break
    }
    if (formModel.value[key] > formKeyMap[key].max) {
      objKey = key
      max = formKeyMap[key].max
      break
    }
  }

  if (objKey && !max) {
    uni.showToast({
      title: `${formKeyMap[objKey].label}不能为空`,
      icon: 'none',
    })
    return false
  }
  if (objKey && max) {
    uni.showToast({
      title: `${formKeyMap[objKey].label}不能超过${max}`,
      icon: 'none',
    })
    return false
  }

  formRef.value
    .validate()
    .then(async () => {
      const params = {
        ...formModel.value,
        userEvaluationId: personId.value,
      }
      const apiFn =
        detailData.value.status === 'start'
          ? AccessApi.submitPersonEarlyScore
          : AccessApi.submitPersonFinallyScore
      const { code, result, message } = await apiFn(params)
      if (code === 200 && result) {
        uni.showToast({
          icon: 'success',
          title: '提交成功',
        })
        setTimeout(() => {
          // isNavigateBack.value = true
          // uni.navigateBack()
          goBack()
        }, 1000)
      } else {
        uni.showToast({
          icon: 'error',
          title: message,
        })
      }
    })
    .catch((error) => {
      console.error(error)
    })
}

/**
 * 如果需要操作完，立马返回上一页，执行更新操作
 */
const goBack = () => {
  const pages = getCurrentPages()
  const prevPage = pages[pages.length - 2]

  uni.navigateBack({
    delta: 1,
    success: () => {
      prevPage.$vm?.updateList()
    },
  })
}

const personId = ref('')
onLoad((options) => {
  if (options.id) {
    personId.value = options.id
    getDetail(options.id)
  }
})
</script>

<script>
export default {
  options: {
    styleIsolation: 'shared',
  },
}
</script>

<style lang="scss">
@import '../../../style/common-form';

.common-info {
  padding-bottom: $uni-spacing-col-lg;

  &-title {
    padding-bottom: $uni-spacing-col-base;
    margin-bottom: $uni-spacing-col-base;
    font-size: $uni-font-size-lg;
    font-weight: 600;
    border-bottom: 1px solid $uni-border-color;
  }

  &-item {
    margin-bottom: $uni-spacing-col-sm;
  }
}
</style>
